package com.kingtone.rongji.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kingtone.rongji.BrowserActivity;
import com.kingtone.rongji.OrderListActivity;
import com.kingtone.rongji.PasswordActivity;
import com.kingtone.rongji.R;
import com.kingtone.rongji.ReceiveAddressActivity;
import com.kingtone.rongji.biz.User;
import com.kingtone.rongji.biz.UserCounts;
import com.kingtone.rongji.network.UserRequest;
import com.kingtone.rongji.tool.AppContasts;
import com.kingtone.rongji.tool.AppRestClient;
import com.kingtone.rongji.tool.SharedPreferencesUtil;
import com.kingtone.rongji.ui.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ProfileFragment extends BaseFragment implements
		View.OnClickListener {

	private String TAG = "ProfileFragment";

	private RoundImageView image;

	private TextView userName;

	private TextView level;

	private TextView money;

	private TextView scored;

	private Button notPay;

	private Button notReceived;

	private User user = null;

	private UserCounts counts = null;

	private void initData() {
		user = null;
		counts = null;
		String userId = SharedPreferencesUtil.getInstance(mActivity).getString(
				AppContasts.USERID_KEY);
		UserRequest userRequest = new UserRequest();
		userRequest.getUserInfo(userId, this);
		userRequest.getUserCounts(userId, this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onCreateView");

		View view = inflater.inflate(R.layout.fragment_profile, null);

		initTitleView(view, getString(R.string.title_profile));
		backBtn.setVisibility(View.GONE);

		LinearLayout pwdContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_pwd);
		pwdContainer.setOnClickListener(this);

		LinearLayout helpContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_help);
		helpContainer.setOnClickListener(this);

		RelativeLayout orderfNotPayContainer = (RelativeLayout) view
				.findViewById(R.id.id_btn_not_pay);
		orderfNotPayContainer.setOnClickListener(this);

		RelativeLayout orderfNotReveiceContainer = (RelativeLayout) view
				.findViewById(R.id.id_btn_not_received);
		orderfNotReveiceContainer.setOnClickListener(this);

		LinearLayout orderfContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_order_all);
		orderfContainer.setOnClickListener(this);

		LinearLayout qbfContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_query);
		qbfContainer.setOnClickListener(this);

		LinearLayout myCouponContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_coupon);
		myCouponContainer.setOnClickListener(this);

		LinearLayout commentContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_comment);
		commentContainer.setOnClickListener(this);

		LinearLayout favoriteContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_fav);
		favoriteContainer.setOnClickListener(this);

		LinearLayout addressContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_address);
		addressContainer.setOnClickListener(this);

		LinearLayout infoContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_info);
		infoContainer.setOnClickListener(this);
		
		LinearLayout callContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_call);
		callContainer.setOnClickListener(this);
		
		LinearLayout feedbackContainer = (LinearLayout) view
				.findViewById(R.id.id_btn_feedback);
		feedbackContainer.setOnClickListener(this);

		image = (RoundImageView) view.findViewById(R.id.img_profile);
		userName = (TextView) view.findViewById(R.id.id_tv_username);
		level = (TextView) view.findViewById(R.id.id_tv_level);
		money = (TextView) view.findViewById(R.id.id_tv_paid);
		scored = (TextView) view.findViewById(R.id.id_tv_score);
		notPay = (Button) view.findViewById(R.id.id_bt_not_pay_number);
		notReceived = (Button) view
				.findViewById(R.id.id_bt_not_received_number);

		Button logout = (Button) view.findViewById(R.id.id_btn_logout);
		logout.setOnClickListener(this);

		if (SharedPreferencesUtil.getInstance(mActivity).getBoolean(
				AppContasts.IS_LOGINED_KEY)) {
			initData();
		}

		return view;
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch (id) {
		case R.id.id_btn_pwd:
			Intent intent = new Intent(this.getActivity(),
					PasswordActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
			break;
		case R.id.id_btn_help:
			BrowserFragment bf = new BrowserFragment();
			bf.setTitle(this.getString(R.string.title_help));
			bf.setUrl(AppRestClient.HELP_URL);
			addFragmentToStack(bf);
			break;
		case R.id.id_btn_not_pay:
			Intent  orderNotPayIntent = new Intent(this.mActivity, OrderListActivity.class);
			orderNotPayIntent.putExtra("flag", AppContasts.ORDER_STATUS_WAITING_PAY);
			startActivity(orderNotPayIntent);
			break;
		case R.id.id_btn_not_received:
			Intent  orderNotIntent = new Intent(this.mActivity, OrderListActivity.class);
			orderNotIntent.putExtra("flag", AppContasts.ORDER_STATUS_WAITING_RECEIVE);
			startActivity(orderNotIntent);
			break;
		case R.id.id_btn_order_all:
			Intent  orderAllIntent = new Intent(this.mActivity, OrderListActivity.class);
			orderAllIntent.putExtra("flag", AppContasts.ORDER_STATUS_ALL);
			startActivity(orderAllIntent);
			break;
		case R.id.id_btn_comment:
			MyCommentsFragment commentsFrag = new MyCommentsFragment();
			addFragmentToStack(commentsFrag);
			break;
		case R.id.id_btn_coupon:
			MyCouponFragment couponFrag = new MyCouponFragment();
			addFragmentToStack(couponFrag);
			break;
		case R.id.id_btn_fav:
			MyFavoriteFragment favFrag = new MyFavoriteFragment();
			addFragmentToStack(favFrag);
			break;
		case R.id.id_btn_query:
			QueryBalanceFragment qbf = new QueryBalanceFragment();
			addFragmentToStack(qbf);
			break;
		case R.id.id_btn_info:
			PersonFragment person = new PersonFragment();
			Bundle bundle = new Bundle();
			bundle.putParcelable("userInfo", user);
			person.setArguments(bundle);
			addFragmentToStack(person);
			break;
		case R.id.id_btn_address:
			Intent addressIntent = new Intent(mActivity,
					ReceiveAddressActivity.class);
			addressIntent.putExtra("pageFlag", 1);
			mActivity.startActivity(addressIntent);
			break;
		case R.id.id_btn_logout:
			mActivity.logout();
			mActivity.setCurrentTab("Dashboard", null);
			break;
		case R.id.id_btn_feedback:
			BrowserFragment feedBack = new BrowserFragment();
			feedBack.setTitle(this.getString(R.string.profile_feedback));
			feedBack.setUrl(AppRestClient.FEEDBACK_URL);
			addFragmentToStack(feedBack);
			break;
		case R.id.id_btn_call:
			Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:4008804365"));  
            startActivity(callIntent);  
			break;
		default:
			break;
		}
	}

	private void addFragmentToStack(BaseFragment newFragment) {
		// TODO Auto-generated method stub
		newFragment.setStackFragment(stackFragment);
		stackFragment.addFragmentToStack(newFragment);
	}

	@Override
	public void onBussinessSuccess(Object obj) {
		// TODO Auto-generated method stub
		super.onBussinessSuccess(obj);
		if (obj instanceof User) {
			user = (User) obj;
			userName.setText(user.getRealName());
			level.setText(user.getRankName());
			money.setText(user.getExpenditure() + "");
			scored.setText(user.getPoints() + "");
			String icon = user.getHeadImgUrl();
			if (icon != null) {
				ImageLoader.getInstance().displayImage(icon, image);
			}
		} else if (obj instanceof UserCounts) {
			counts = (UserCounts) obj;
			int count = counts.getDaiZhiFuOrderCount();
			if (count > 0) {
				notPay.setText("" + counts.getDaiZhiFuOrderCount());
				notPay.setVisibility(View.VISIBLE);
			} else {
				notPay.setVisibility(View.GONE);
			}

			count = counts.getYiFaHuoOrderCount();
			if (count > 0) {
				notReceived.setText("" + count);
				notReceived.setVisibility(View.VISIBLE);
			} else {
				notReceived.setVisibility(View.GONE);
			}
		}
	}
}
